#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <pspdisplay.h>
#include <stdio.h>
#include <pspmoduleinfo.h>
#include <psppower.h>
#include "Logger/loggerc.h"


/* Define the module info section */
PSP_MODULE_INFO("logger_sample", 0, 0, 71);

/* Define printf, just to make typing easier */
#define printf  pspDebugScreenPrintf

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
    sceKernelExitGame();
	return 0;
}


/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
    int cbid;
    cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
    sceKernelRegisterExitCallback(cbid);
    sceKernelSleepThreadCB();

	return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
    int thid = 0;
    thid = sceKernelCreateThread("logger_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
    if (thid >= 0)
	sceKernelStartThread(thid, 0, 0);
    return thid;
}

/* main routine */
int main(int argc, char *argv[])
{
    SceCtrlData pad;
    u32 oldButtons = 0;

	int filelog = initLog("./test.log",500,NODEBUG);

	writelog( filelog, INF, "init screen and callbacks\n");
    //init screen and callbacks
    pspDebugScreenInit();
    pspDebugScreenClear();
    SetupCallbacks();

	writelog( filelog, INF, "SetupPad\n");
    // Setup Pad
    sceCtrlSetSamplingCycle(0);
    sceCtrlSetSamplingMode(0);


	writelog( filelog, INF, "Enter Main Loop\n");
    //fetch and print out power and battery information
    for (;;) {
		pspDebugScreenSetXY(0, 0);
		sceCtrlReadBufferPositive(&pad, 1);

		printf("Press X to write INF record\n");
		printf("Press O to write DBG record\n");
		printf("Press [] to write ERR record\n");
		printf("Press /\\ to write WRN record\n");
		printf("Press LTRIGGER to enable DEBUG log\n");
		printf("Press RTRIGGER to disable DEBUG log\n");

		//only check buttons if they've changed since last frame
		if (oldButtons != pad.Buttons) {
			writelog( filelog, INF, "Check pressed Button\n");
			if (pad.Buttons & PSP_CTRL_CROSS)
				writelog( filelog, INF, "Pressed CROSS\n");
			else 
			if (pad.Buttons & PSP_CTRL_CIRCLE)
				writelog( filelog, DBG, "Pressed CIRCLE\n");
			else
			if (pad.Buttons & PSP_CTRL_SQUARE)
				writelog( filelog, ERR, "Pressed SQUARE\n");
			else
			if (pad.Buttons & PSP_CTRL_TRIANGLE)
				writelog( filelog, WRN, "Pressed TRIANGLE\n");
			else
			if (pad.Buttons & PSP_CTRL_RTRIGGER) {
				setDebugOFF();
				writelog( filelog, INF, "Pressed RTRIGGER - Disable DEBUG\n");
			} 
			else
			if (pad.Buttons & PSP_CTRL_LTRIGGER) {
				setDebugON();
				writelog( filelog, INF, "Pressed LTRIGGER - Enable DEBUG\n");
			}
			else 
			if (pad.Buttons & PSP_CTRL_START)
				deinitLog(filelog);
			
		}
		oldButtons = pad.Buttons;
		sceDisplayWaitVblankStart();
    }
	deinitLog(filelog);

    sceKernelSleepThread();
    sceKernelExitGame();

    return 0;
}
